package git.soulbgm.security.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

import static git.soulbgm.security.constant.SecurityConstants.KEY_PREFIX_LOGIN_FAILURE;

/**
 * 身份验证成功的监听器
 *
 * @author 贺瑞杰
 * @date 2021/01/07
 */
@Component
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired
    private StringRedisTemplate redis;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        String username = event.getAuthentication().getPrincipal().toString();
        redis.delete(KEY_PREFIX_LOGIN_FAILURE + username);
    }
}
